import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.ensemble import ExtraTreesClassifier

# 读取CSV文件
df = pd.read_csv(r'D:\kaggle\cancercsv\cancer.csv')
# df = pd.read_csv(r'D:\kaggle\final\cancers1.csv')

# 将所有列转换为数值类型
df = df.apply(pd.to_numeric, errors='coerce')

# 删除Number列
df = df.drop(columns=['Number'])

# 处理缺失值
df.fillna(df.mean(), inplace=True)

# 对数据进行范围限制
df['SCC'] = df['SCC'].clip(0, 1.95)
df['CEA'] = df['CEA'].clip(0, 5)
df['CK19'] = df['CK19'].clip(0, 3.3)
df['NSE'] = df['NSE'].clip(0, 17)
df['ProGRP'] = df['ProGRP'].clip(0, 0.1)
df['CTn1'] = df['CTn1'].clip(0.014, 0.429)
df['PSA(L)'] = df['PSA(L)'].clip(0, 0.93)
df['CA125'] = df['CA125'].clip(0, 35)
df['CA153'] = df['CA153'].clip(0, 35)
df['PSA'] = df['PSA'].clip(0, 4)
df['AFP'] = df['AFP'].clip(0, 20)
df['PG1'] = df['PG1'].clip(30, 100)  # 假设上限为100
df['PG1/PG2'] = df['PG1/PG2'].clip(3, 100)  # 假设上限为100
df['CA199'] = df['CA199'].clip(0, 35)
df['CA724'] = df['CA724'].clip(0, 6.9)
df['TH'] = df['TH'].clip(0.75, 2.38)
df['TH(y)'] = df['TH(y)'].clip(4.72, 13.2)
df['T3'] = df['T3'].clip(0.7, 1.48)
df['T3(y)'] = df['T3(y)'].clip(1.58, 3.91)
df['TSH'] = df['TSH'].clip(0.3, 5)
df['Blood pressure(high)'] = df['Blood pressure(high)'].clip(90, 139)
df['Blood pressure(low)'] = df['Blood pressure(low)'].clip(60, 89)

# 计算相关性矩阵
correlation_matrix = df.corr()
print(correlation_matrix['Lung_cancer'])

# 计算各因子的重要性排序
X = df.drop(columns=['Lung_cancer'])  # 独立变量
y = df['Lung_cancer']  # 目标变量，即Lung_cancer
model = ExtraTreesClassifier()
model.fit(X, y)
print(model.feature_importances_)  # 使用树模型的内置特征重要性

# 可视化特征重要性
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.xlabel('Feature Importance')
plt.ylabel('Features')
plt.title('Feature Importance Ranking for Lung Cancer')
plt.show()

# 绘制相关性热力图
plt.figure(figsize=(24, 20))  # 增加图的宽度和高度
heatmap = sns.heatmap(correlation_matrix, annot=False, cmap='coolwarm', fmt='.2f', linewidths=0.5)
plt.title('Correlation Matrix Heatmap', fontsize=20)
plt.xticks(fontsize=10, rotation=90)  # 旋转 x 轴标签以避免重叠
plt.yticks(fontsize=10)

# 手动添加注释文本
for i in range(correlation_matrix.shape[0]):
    for j in range(correlation_matrix.shape[1]):
        plt.text(j + 0.5, i + 0.5, f'{correlation_matrix.iloc[i, j]:.2f}',
                 ha='center', va='center', fontsize=8, color='black')

plt.show()